package com.xbxkj.commen.serviceImpl;

import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbxkj.commen.bean.AdminUser;
import com.xbxkj.commen.bean.User;
import com.xbxkj.commen.controller.helper.$;
import com.xbxkj.commen.controller.helper.V;
import com.xbxkj.commen.controller.model.ApiResultModel;
import com.xbxkj.commen.controller.model.ResultModel;
import com.xbxkj.commen.mapper.UserMapper;
import com.xbxkj.commen.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbxkj.commen.bean.layuiBean.ResultPage;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商城小程序用户 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2019-12-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
     * 添加User
     *
     * @param userInfo 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel<Object> addUser(WxMaUserInfo userInfo) {
        User user ;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("openid", userInfo.getOpenId());
        user = getOne(queryWrapper);
        if (user != null) {
             setUser(user, userInfo);
            updateById(user);
        } else {
            user = new User();
            save(setUser(user, userInfo));
        }
        return $.success();
    }

    private User setUser(User user, WxMaUserInfo userInfo) {
        user.setCity(userInfo.getCity());
        user.setCountry(userInfo.getCountry());
        user.setHeadimgurl(userInfo.getAvatarUrl());
        user.setNickname(userInfo.getNickName());
        user.setOpenid(userInfo.getOpenId());
        user.setProvince(userInfo.getProvince());
        user.setSex(userInfo.getGender());
        return user;
    }

    /**
     * 编辑跳转页面的回显数据User
     *
     * @param id 前端传进的参数
     * @return 返回实体类
     */
    @Override
    public User selectUserByIdUser(String id) {
        return this.getById(id);
    }


    /**
     * 编辑User
     *
     * @param user 前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel<Object> editUser(User user) {
        if (this.updateById(user)) {
            return $.success();
        } else {
            return $.error("编辑失败");
        }
    }

    /**
     * 删除一条User
     *
     * @param id 前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel<Object> delUser(String id) {
        if (this.removeById(id)) {
            return $.success();
        } else {
            return $.error("删除失败");
        }
    }

    /**
     * 分页查询User
     *
     * @param page  页码
     * @param limit 查几条
     * @param user  前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findUserByPage(Integer page, Integer limit, User user) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("user", user);
        List<User> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<User> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


}
